package com.hhm.huo.controller;

import com.hhm.huo.entity.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @RequestMapping("foretPassword")
    public String foretPassword(){
        return "";
    }

    /**
     * 登录成功
     * @return
     */
    @RequestMapping("loginOk")
    public Result loginOk(){
        //获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //验证 principal 是否是 UserDetails
        // UserDetails 是保存用户信息的实体bean
        if (principal instanceof UserDetails){
            return new Result(200, "登陆成功", principal);
        }

        return new Result(201, "登陆失败", "");
    }

    /**
     * 登录失败
     * @return
     */
    @RequestMapping("loginNo")
    public Result loginNo(){
        //获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //验证 principal 是否是 UserDetails
        // UserDetails 是保存用户信息的实体bean
        if (principal instanceof UserDetails){
            return new Result(200, "登陆成功", principal);
        }

        return new Result(201, "登陆失败", "");
    }
    /**
     * 登出
     * @return
     */
    @RequestMapping("logout")
    public Result logout(){
            return new Result(200, "登出成功", "注销");

    }

    /**
     * 未登录
     * FORBIDDEN 403
     * @return
     */
    @RequestMapping("NoLogin")
    public ResponseEntity NoLogin(){


        return new ResponseEntity(new Result(300, "登陆失败", ""),HttpStatus.FORBIDDEN);
    }
}
